//
//  CommodityDetailViewController.m
//  HWZB
//
//  Created by 许成雄 on 2017/4/26.
//  Copyright © 2017年 wonders. All rights reserved.
//

#import "CommodityDetailViewController.h"
#import "PackageInfo.h"
#import "ProductInfo.h"
#import "PackageTableViewCell.h"
#import "CommodityScrollTableViewCell.h"
#import "PackageDetailViewController.h"
#import "PurchaseDetailViewController.h"
#import "HWZBAPIRequest.h"
#import "Context.h"
#import "NSString+Utils.h"
#import "RegexKit.h"
#import "MBProgressHUD+Toast.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import <UIImageView+WebCache.h>

@interface CommodityDetailViewController () <UITableViewDelegate, UITableViewDataSource, CommodityScrollTableViewCellDelegate>

@property (strong, nonatomic) UIImageView *pictureImageView;
@property (strong, nonatomic) UILabel *commodityLabel;
@property (strong, nonatomic) UILabel *priceLabel;
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) UIButton *buyButton;

@property (strong, nonatomic) NSMutableArray *imageUrlArray;
@property (strong, nonatomic) NSMutableArray *packageArray;
@property (strong, nonatomic) CommodityDetailInfo *commodityInfo;

@end

@implementation CommodityDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = I_COLOR_BACKGROUND;
    self.navigationItem.title = @"商品详情";
    [self setBackNavgationItem];
    
    [self createUI];
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - UIButton Action
- (void)buyButtonAction {
    //TODO -- 立即购买按钮点击事件
    PurchaseDetailViewController *purchaseDetailViewController = [[PurchaseDetailViewController alloc] init];
    [self.navigationController pushViewController:purchaseDetailViewController animated:YES];
}

#pragma mark - UITableViewDelegate && UITableViewDatasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if(section == 0) {
        return 0.0f;
    } else {
        return TRANS_VALUE(8.0f);
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if(section == 0) {
        return [[UIView alloc] initWithFrame:CGRectZero];
    } else {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, TRANS_VALUE(8.0f))];
        view.backgroundColor = I_COLOR_BACKGROUND;
        return view;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0) {
        return 1;
    } else {
        NSInteger count = self.packageArray != nil ? self.packageArray.count : 0;
        return count;
    }
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 0) {
        CGFloat height = TRANS_VALUE(140.0f);
        return height;
    } else if(indexPath.section == 1) {
        CGFloat height = TRANS_VALUE(90.0f);
        return height;
    } else {
        return 0.0f;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 0) {
        static NSString *identifier = @"CommodityScrollTableViewCell";
        CommodityScrollTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if(!cell) {
            cell = [[CommodityScrollTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        }
        if(indexPath.row < 1) {
            //TOOD -- 滚动图片
            cell.delegate = self;
            cell.topList = self.imageUrlArray;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else if(indexPath.section == 1) {
        static NSString *identifier = @"PackageTableViewCell";
        PackageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if(!cell) {
            cell = [[PackageTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        }
        if(indexPath.row < [self.packageArray count]) {
            //TOOD -- 发现列表
            PackageInfo *packageInfo = (PackageInfo *)[self.packageArray objectAtIndex:indexPath.row];
            cell.packageInfo = packageInfo;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        return nil;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //跳转到套餐详情页面
    if(indexPath.section == 1) {
        PackageDetailViewController *packageDetailViewController = [[PackageDetailViewController alloc] init];
        PackageInfo *packageInfo = (PackageInfo *)[self.packageArray objectAtIndex:indexPath.row];
        packageDetailViewController.packageInfo = packageInfo;
        packageDetailViewController.commodityInfo = self.commodityInfo;
        [self.navigationController pushViewController:packageDetailViewController animated:YES];
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 1) {
        if(indexPath.row < self.packageArray.count) {
            if([cell respondsToSelector:@selector(setSeparatorInset:)]) {
                [cell setSeparatorInset:UIEdgeInsetsZero];
            }
            if([cell respondsToSelector:@selector(setLayoutMargins:)]) {
                [cell setLayoutMargins:UIEdgeInsetsZero];
            }
        } else {
            if([cell respondsToSelector:@selector(setSeparatorInset:)]) {
                [cell setSeparatorInset:UIEdgeInsetsZero];
            }
            if([cell respondsToSelector:@selector(setLayoutMargins:)]) {
                [cell setLayoutMargins:UIEdgeInsetsZero];
            }
        }

    }
}

#pragma mark - Private Method
- (void)createUI {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64.0f) style:UITableViewStylePlain];
    self.tableView.backgroundColor = I_COLOR_WHITE;
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView registerClass:[CommodityScrollTableViewCell class] forCellReuseIdentifier:@"CommodityScrollTableViewCell"];
    [self.tableView registerClass:[PackageTableViewCell class] forCellReuseIdentifier:@"PackageTableViewCell"];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    //设置tableview分割线
    if([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)loadData {
    if(self.commodityId != nil) {
        [SVProgressHUD showWithStatus:@"正在获取商品详情..." maskType:SVProgressHUDMaskTypeClear];
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
        [params setObject:self.commodityId forKey:@"id"];
        [[HWZBAPIRequest sharedInstance] getCommodityDetailWithParams:params success:^(CommodityDetailInfo *commodityInfo) {
            [SVProgressHUD dismiss];
            if(commodityInfo != nil) {
                [self showCommodityDetailInfo:commodityInfo];
            } else {
                [self showCommodityDetailInfo:nil];
            }
        } failure:^(NSError *error) {
            [SVProgressHUD dismiss];
            [self showCommodityDetailInfo:nil];
            [MBProgressHUD showToast:[error localizedDescription] initWithView:self.view];
        }];
    }
    
   [self.tableView reloadData];
}

- (void)showCommodityDetailInfo:(CommodityDetailInfo *)commodityInfo {
    self.commodityInfo = commodityInfo;
    if(commodityInfo != nil) {
        if(!self.imageUrlArray) {
            self.imageUrlArray = [[NSMutableArray alloc] initWithCapacity:0];
        }
//        [self.imageUrlArray removeAllObjects];
//        if(commodityInfo.imageUrl != nil) {
//            [self.imageUrlArray addObject:commodityInfo.imageUrl];
//        }
//        if(commodityInfo.imageUrl2 != nil) {
//            [self.imageUrlArray addObject:commodityInfo.imageUrl2];
//        }
//        if(commodityInfo.imageUrl3 != nil) {
//            [self.imageUrlArray addObject:commodityInfo.imageUrl3];
//        }
//        if(commodityInfo.imageUrl4 != nil) {
//            [self.imageUrlArray addObject:commodityInfo.imageUrl4];
//        }
//        if(commodityInfo.imageUrl5 != nil) {
//            [self.imageUrlArray addObject:commodityInfo.imageUrl5];
//        }
        if(commodityInfo.packageList != nil) {
            for(PackageInfo *packageInfo in commodityInfo.packageList) {
                if(packageInfo.productArray != nil) {
                    for(int i = 0, n = (int)packageInfo.productArray.count; i < n; i++) {
                        ProductInfo *productInfo = [packageInfo.productArray objectAtIndex:i];
                        if(productInfo.imageUrl != nil) {
                            [self.imageUrlArray addObject:productInfo.imageUrl];
                        }
                    }
                }
            }
        }
        
        if(!self.packageArray) {
            self.packageArray = [[NSMutableArray alloc] initWithCapacity:0];
        }
        [self.packageArray removeAllObjects];
        NSArray *packageArray = commodityInfo.packageList;
        if(packageArray != nil && packageArray.count > 0) {
            [self.packageArray addObjectsFromArray:packageArray];
        }
        [self.tableView reloadData];
    } else {
        //什么都不显示
    }
}


@end
